package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.WebComment;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 网站评论
 * TableName : WEB_COMMENT
 * ClassName : com.swlz.dao.WebCommentDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-23 20:41:10
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class WebCommentDAO {

	/**
	 * 添加一个网站评论
	 * 包含所有字段
	 * @param webComment - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(WebComment webComment) throws SQLException {
		if(webComment.getCommentId()==null) {
			webComment.setCommentId(DBUtil.nextId());
		}
		return DBUtil.insert(webComment);
	}
 
	
	/**
	 * 添加一个网站评论
	 * 只包含非null的字段
	 * @param webComment - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(WebComment webComment) throws SQLException {
		if(webComment.getCommentId()==null) {
			webComment.setCommentId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(webComment);
	}
	

	/**
	 * 根据主键更新一个网站评论
	 * 包含所有字段
	 * @param webComment - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(WebComment webComment) throws SQLException {
		return DBUtil.update(webComment);
	}
	
	/**
	 * 根据主键更新一个网站评论
	 * 只包含非null的字段
	 * @param webComment - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(WebComment webComment) throws SQLException {
		return DBUtil.updateSelective(webComment);
	}
	
	/**
	 * 根据主键删除一个网站评论
	 * @param commentId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long commentId) throws SQLException  {
		return DBUtil.execute("DELETE FROM WEB_COMMENT WHERE COMMENT_ID=?", commentId);
	}
	
	/**
	 * 根据主键获取一个网站评论
	 * @param commentId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public WebComment selectById(Long commentId) throws SQLException  {
		return DBUtil.get("SELECT * FROM WEB_COMMENT WHERE COMMENT_ID=?", WebComment.class ,commentId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param commentId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public WebComment selectByModel(WebComment webComment)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM WEB_COMMENT WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(webComment,sql,params);
		return DBUtil.get(sql.toString(), WebComment.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param commentId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<WebComment> selectByModel(WebComment webComment,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM WEB_COMMENT WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(webComment,sql,params);
		sql.append(" ORDER BY COMMENT_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), WebComment.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param commentId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(WebComment webComment)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM WEB_COMMENT WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(webComment,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param commentId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(WebComment webComment,StringBuffer sql,List<Object> params) {
		if(webComment==null){
			return;
		}

		if(webComment.getCommentId()!=null) {
			sql.append(" AND COMMENT_ID=?");
			params.add(webComment.getCommentId());
		}

		if(webComment.getCommentParentId()!=null) {
			sql.append(" AND COMMENT_PARENT_ID=?");
			params.add(webComment.getCommentParentId());
		}

		if(webComment.getCommentType()!=null) {
			sql.append(" AND COMMENT_TYPE=?");
			params.add(webComment.getCommentType());
		}

		if(webComment.getCommentTypeId()!=null) {
			sql.append(" AND COMMENT_TYPE_ID=?");
			params.add(webComment.getCommentTypeId());
		}

		if(webComment.getCommentUser()!=null) {
			sql.append(" AND COMMENT_USER=?");
			params.add(webComment.getCommentUser());
		}

		if(webComment.getCommentTime()!=null) {
			sql.append(" AND COMMENT_TIME=?");
			params.add(webComment.getCommentTime());
		}

		if(webComment.getCommentLevel()!=null) {
			sql.append(" AND COMMENT_LEVEL=?");
			params.add(webComment.getCommentLevel());
		}

		if(webComment.getCommentContent()!=null) {
			sql.append(" AND COMMENT_CONTENT=?");
			params.add(webComment.getCommentContent());
		}

	}
}